<?php
session_start();
if (!isset($_SESSION['love'])) {
  header('Location: ./login.php');
}
?>
<div class="table-responsive">
  <table class="table">
    <thead>
      <tr>
        <th>姓名</th>
        <th>时间</th>
        <th>赞量</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
<?php
//删除数据
if (isset($_GET['id'])) {
  $id = $_GET['id'];
  DB::query("DELETE FROM love_list WHERE Id = '$id'");
  echo '<script>layer.confirm("删除成功！", {
    btn: ["确定"],
    btn1:function(index,layero)
    {
      window.location.href="./?s=list&page='.$_GET['page'].'";
    }
  });</script>';
}
//判断是否有分页参数
if (!isset($_GET['page'])) {
  header('Location: ?s=list&page=1');
}
//获取数据
$p = ($_GET['page']-1)*8;
$result = DB::query("SELECT * FROM love_list ORDER BY Id DESC limit $p, 8");
  if (DB::affected($result) > 0) {
    // 输出数据
    while($row = DB::fetch($result)) {
      echo '<tr><td>'.htmlspecialchars($row['name1']).'&'.htmlspecialchars($row['name2']).'</td><td>'.$row['time'].'</td><td>'.$row['s'].'</td><td><a href="./?s=list&page='.$_GET['page'].'&id='.$row['Id'].'" class="btn btn-xs btn-danger">删除</a></td></tr>';
    }
  }
$page = $_GET['page'];
?>
    </tbody>
</table>
</div>
<ul class="pagination">
<li><a href="./?s=list&page=1">&laquo;</a></li>
<?php
//四舍五入，循环输出页
$num = round($s/8);
for ($i=1; $i < $num+1; $i++) {
  if ($page == $i) {
    echo '<li class="active"><a href="./?s=list&page='.$i.'">'.$i.'</a></li>';;
  }else{
    echo '<li><a href="./?s=list&page='.$i.'">'.$i.'</a></li>';
  }
}
?>
<li><a href="./?s=list&page=<?=$num?>">&raquo;</a></li>
</ul>
<script>$("#list").attr("class","active");</script>